/*
 * $Header: /u/home/public/Caddy/CVS_ROOT/CaddyWeb_UpdateRoutines/src/ca/caddy/caddyweb/powertool/BeanDataShow.java,v 1.1 2007/02/12 20:51:04 frankish Exp $ 
 */
package net.frankish.tools;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

/**
 * 
 * <p>Title: $RCSfile: BeanDataShow.java,v $</p>
 * File: BeanDataShow.java
 * Project: CaddyGuiTool
 * @author Frankish
 * @version $Revision: 1.1 $
 */
public class FrankishDataPanel extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static Logger logger = Logger.getLogger(FrankishDataPanel.class.getName());

	private ArrayList objects;
	
	private Object bean = null;
	Method[] methods = null;
	HashMap fieldMap = new HashMap();
	HashMap componentMap = new HashMap();

	JScrollPane scrollPane = null;
	
	public FrankishDataPanel(){
		this.setLayout(new GridBagLayout());
		this.setBorder(BorderFactory.createTitledBorder("DataBean Display"));
	}
	
	public void setObject(Object bean){
		this.bean = bean;
		this.methods = bean.getClass().getMethods();
		for(int i = 0; i < methods.length; i++){
			Method met = methods[i];
			String name = met.getName();
			if(!met.getDeclaringClass().equals(bean.getClass())) //We avoid parent's methods...
				continue;
			
			if(name.startsWith("get")){
				String field = name.substring(3);
				try {
					Object value = met.invoke(bean, null);
					fieldMap.put(field, value);
				} catch (IllegalArgumentException e) {
					logger.severe("Field["+name+"] couldnt be mapped");
				} catch (IllegalAccessException e) {
					logger.severe("Field["+name+"] couldnt be mapped");
				} catch (InvocationTargetException e) {
					logger.severe("Field["+name+"] couldnt be mapped");
				}
			}
		}
		display();
	}
	
	public void setObjects(ArrayList items){
		if(scrollPane != null)
			this.remove(scrollPane);
		if(items == null) items = new ArrayList();
		this.objects = items;
		String[] arr = new String[this.objects.size()];
		for(int i = 0; i < this.objects.size();i++){
			arr[i] = this.objects.get(i).toString();
		}
		JList list = new JList(arr);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane = new JScrollPane(list);
		
		//What size?
		int mets = 1;
		if(objects.size() > 0){
			mets = objects.get(1).getClass().getMethods().length;
		}
		
		this.add(scrollPane,new GridBagConstraints(0, 0, 1,mets , 0.5, 0.5, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
	}
	
	private void display() {
		Iterator it = fieldMap.keySet().iterator();
		componentMap.clear();
		this.removeAll();
		while(it.hasNext()){
			String field = (String)it.next();
			String value = null;
			try{
				value = fieldMap.get(field).toString();
			}catch(NullPointerException ex){
				value = "";
			}
			if(bean instanceof FrankishDisplayDescriptor){
				FrankishDisplayDescriptor desc = (FrankishDisplayDescriptor)bean;
				JComponent comp = desc.retrieveComponentForField(field);
				componentMap.put(field, comp);
				int index = desc.retrieveIndexForField(field);
				this.add(new JLabel(desc.retrieveTitleForField(field)),new GridBagConstraints(1, index, 1, 1, 0.2, 0.2, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
				if(comp instanceof JTextArea)comp = new JScrollPane(comp);
				this.add(comp,new GridBagConstraints(2, index, 1, 1, 0.3, 0.3, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
			}else{
				JTextField comp = new JTextField();
				comp.setText(value);
				comp.setEnabled(false);
				comp.setEditable(false);
				comp.setPreferredSize(new Dimension(150,27));
				this.add(new JLabel(field),new GridBagConstraints(1, -1, 1, 1, 0.2, 0.2, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
				this.add(comp,new GridBagConstraints(2, -1, 1, 1, 0.3, 0.3, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
			}
		}
	}
	
	public void addControlPanel(){
		//TODO add the controls Apply et Cancel
	}

	/**
	 * 
	 * @param 
	 * @return void 
	 * @exception
	 * @author Frankish
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		FrankishDataPanel pane = new FrankishDataPanel();
		frame.getContentPane().add(pane);
		frame.pack();
		frame.setVisible(true);
	}
}
